﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

#region Answer
namespace backoffice.Data
{
    partial class Answer
    {
        public void setAnswer(Answer a)
        {
            this.AnswerID = a.AnswerID;
            this.Date = a.Date;
            this.ExpertID = a.ExpertID; 
            this.QuestionID = a.QuestionID;
            this.Text = a.Text;
        }
    }
}
#endregion

#region dataAnswer
namespace backoffice.Data
{
    public class dataAnswer
    {
        #region Create
        public static void createAnswer(Answer a)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                a.AnswerID = Guid.NewGuid();
                a.Date = DateTime.Now;

                db.Answers.InsertOnSubmit(a);
                db.SubmitChanges();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Get
        public static Answer getAnswer(Guid guid)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                Answer answer = (from a in db.Answers
                                 where a.AnswerID == guid
                                 select a).FirstOrDefault();

                return answer;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static Answer getAnswer(Expression<Func<Answer, bool>> whereClause)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Answers.Where(whereClause).FirstOrDefault();
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Answer> getAllAnswers()
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Answers;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Answer> getAnswers(Expression<Func<Answer, bool>> whereClause)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Answers.Where(whereClause);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Set
        public static Boolean changeAnswer(Answer a)
        {
            try
            {
                if (a == null) return false;
                else
                {
                    db2growDataContext db = new db2growDataContext();

                    Answer res = db.Answers.FirstOrDefault(ans => ans.AnswerID == a.AnswerID);
                    if (res == null) return false;
                    else
                    {
                        res.setAnswer(a);
                        db.SubmitChanges();
                        return true;
                    }
                }
                
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Delete
        public static Boolean deleteAnswer(Answer a)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                if (db.Answers.Contains(a))
                {
                    Answer res = db.Answers.FirstOrDefault(ans => ans.AnswerID == a.AnswerID);

                    if (res == null) return false;
                    else
                    {
                        db.Answers.DeleteOnSubmit(res);
                        db.SubmitChanges();
                        return true;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion
    }
}
#endregion